'use strict'

const path = require('path')

const Koa = require('koa')
const views = require('koa-views')
const koaBody = require('koa-body')
const logger = require('koa-logger')
const Router = require('koa-router')

const cfg = require('./config')

const app = new Koa()
const router = new Router()

var SSOController = require('./controllers/sso')

router
// 同域sso
.get('/sameDomain/login', async function(ctx,next){
  await ctx.render('login',{title:'用户登陆'})
})
.post('/sameDomain/login',SSOController.login)
.get('/sameDomain/demo1',SSOController.renderDemo1)
.get('/sameDomain/demo2',SSOController.renderDemo2)
// 同父域
.get('/sameFather/login',async function(ctx,next){
  await ctx.render('login2',{title:'同父域SSO，用户登陆'})
})
.post('/sameFather/login',SSOController.sameFatherLogin)
.get('/sameFather/demo1',SSOController.sameFatherRenderDemo)
.get('/sameFather/demo2',SSOController.sameFatherRenderDemo)
.get('/sameFather/checkCookie',SSOController.checkCookie)

app
.use(logger())
.use(koaBody())
.use(views(path.join(__dirname,'views','pages'),{extension:'pug'}))
.use(router.routes())
.use(router.allowedMethods())
.listen(cfg.port)

console.log('server started at port', cfg.port)